<?php
/**
 * pdo mysqlアダプタ拡張
 * @author tanaka
 * @package Db_Adapter
 * @subpackage PdoMysql
 */
class Db_Adapter_PdoMysql extends Zend_Db_Adapter_Pdo_Mysql
{
	/**
	 * replace table row
	 * @param string $table
	 * @param array $bind
	 * @return
	 */
	public function replace($table, array $bind)
	{
		// extract and quote col names from the array keys
		$cols = array();
		$vals = array();
		foreach ($bind as $col => $val) {
			$cols[] = $this->quoteIdentifier($col, true);
			if ($val instanceof Zend_Db_Expr) {
				$vals[] = $val->__toString();
				unset($bind[$col]);
			}
			else {
				$vals[] = '?';
			}
		}
		
		// build the statement
		$sql = "REPLACE INTO "
			. $this->quoteIdentifier($table, true)
			. ' ('
			. implode(', ', $cols)
			. ') '
			. 'VALUES ('
			. implode(', ', $vals)
			. ')';
		
		// execute the statement and return the number of affected rows
		$stmt = $this->query($sql, array_values($bind));
		$result = $stmt->rowCount();
		return $result;
	}
}